import 'dart:async';
import 'package:flutter/material.dart';
import 'package:kuaitong/units/screen_units.dart';

class ScreenPage extends StatefulWidget {
  @override
  _ScreenPageState createState() => _ScreenPageState();
}

class _ScreenPageState extends State<ScreenPage> {
  int time = 5;
  Timer countdownTimer;
  @override
  void initState() {
    super.initState();
    countdownTimer = Timer.periodic(Duration(milliseconds: 1000), (timer) {
      time-=1;
      if (time <= 0) {
        countdownTimer.cancel();
        Navigator.pushReplacementNamed(context, '/index');
      }
      setState(() {});
    });
  }
  @override
  Widget build(BuildContext context) {
    final screen = Screen(context);
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(color: Colors.white),
        child: Stack(
          children: [
            Positioned(
                top: screen.salc(300),
                left: screen.salc((750 - 200) / 2),
                child: Image.asset(
                  'assets/screen-1.png',
                  width: screen.salc(200),
                )),
            Positioned(
                bottom: screen.salc(200),
                left: screen.salc((750 - 300) / 2),
                child: Image.asset(
                  'assets/screen.png',
                  width: screen.salc(300),
                )),
            Positioned(
                top: screen.salc(80),
                right: screen.salc(30),
                child: Container(
                  width: screen.salc(80),
                  height: screen.salc(40),
                  decoration: BoxDecoration(
                      color: Color.fromARGB(100, 0, 0, 0),
                      borderRadius: BorderRadius.circular(3)),
                  child: Center(
                      child: GestureDetector(
                    onTap: () {
                      countdownTimer.cancel();
                      Navigator.pushReplacementNamed(context, '/index');
                    },
                    child: Text(
                      "$time 秒",
                      style: TextStyle(
                          color: Colors.white, fontSize: screen.salc(25)),
                    ),
                  )),
                ))
          ],
        ),
      ),
    );
  }
}
//没有倒计时的
//class ScreenPage extends StatelessWidget {
//  @override
//  Widget build(BuildContext context) {
//    final screen = Screen(context);
//    Timer(Duration(seconds: 3), (){
//      Navigator.pushReplacementNamed(context, '/home');
//    });
//    return Container(
//      decoration: BoxDecoration(color: Colors.white),
//      child: Stack(
//        children: [
//          Positioned(
//              top: screen.salc(300),
//              left: screen.salc((750 - 200) / 2),
//              child: Image.asset(
//                'assets/screen-1.png',
//                width: screen.salc(200),
//              )),
//          Positioned(
//              bottom: screen.salc(200),
//              left: screen.salc((750 - 300) / 2),
//              child: Image.asset(
//                'assets/screen.png',
//                width: screen.salc(300),
//              )),
//        ],
//      ),
//    );
//  }
//}
